package com.tensquare.article.controller;

import com.baomidou.mybatisplus.plugins.Page;
import com.tensquare.article.pojo.Article;
import com.tensquare.article.service.ArticleService;
import entity.PageResult;
import entity.Result;
import entity.StatusCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/article")
@CrossOrigin
public class ArticleController {
    @Autowired
    private ArticleService articleService;

   // PUT/article/examine/{articleId}文章审核
    @RequestMapping(value = "/examine/{articleId}",method = RequestMethod.PUT)
    public Result Updateexamine(@PathVariable String articleId,
                                @RequestBody Article article){
        article.setId(articleId);
        articleService.updateExamineById(article);
        return new Result(true,StatusCode.OK,"ok");
    }

   // PUT/article/thumbup/{articleId}点赞
    @RequestMapping(value = "/thumbup/{articleId}",method = RequestMethod.PUT)
    public Result addthumbup(@PathVariable String articleId){
        articleService.addthumbup(articleId);
        return new Result(true,StatusCode.OK,"ok");
    }

   // POST/article/search/{page}/{size}文章分页
    @RequestMapping(value = "search/{page}/{size}",method = RequestMethod.POST)
    public Result findByPage(@PathVariable Integer page,
                             @PathVariable Integer size,
                             @RequestBody Map<String,Object> map){
       Page<Article> pageData= articleService.findByPage(map,page,size);
       PageResult<Article> pageResult=new PageResult<>(
                pageData.getTotal(),pageData.getRecords());
       return new Result(true,StatusCode.OK,"ok",pageResult);

    }


    //DELETE/article/{articleId}根据ID删除文章
    @RequestMapping(value = "{articleId}",method = RequestMethod.DELETE)
    public Result deleteById(@PathVariable String articleId){
        articleService.deleteById(articleId);
        return new Result(true,StatusCode.OK,"ok");
    }

    //PUT/article/{articleId}修改文章
    @RequestMapping(value = "{articleId}" ,method = RequestMethod.PUT)
    public Result updateById(@PathVariable String articleId,
                             @RequestBody Article article){
        article.setId(articleId);
        articleService.updateById(article);
        return new Result(true,StatusCode.OK,"修改成功");

    }

    //POST/article增加文章
    @RequestMapping(method = RequestMethod.POST)
    public Result save(@RequestBody Article article){
        articleService.save(article);
        return new Result(true,StatusCode.OK,"保存成功");
    }

    //GET/article/{articleId}根据ID查询文章
    @RequestMapping(value = "{articleId}",method = RequestMethod.GET)
    public Result findById(@PathVariable String articleId){
        Article article= articleService.findById(articleId);
        return new Result(true,StatusCode.OK,"查询成功",article);
    }

    //GET/article文章全部列表
    @RequestMapping(method = RequestMethod.GET)
    public Result findAll(){
        List<Article> list=articleService.findAll();
        return new Result(true, StatusCode.OK,"查询成功",list);
    }

    @RequestMapping(value="/exception", method = RequestMethod.GET)
    public Result exception() throws Exception {
        throw new Exception("测试统一异常处理"); }

}
